"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.upload_server = void 0;
const path = require("path");
const fs = require("fs");
const express = require("express");
const Config_1 = require("../src/config/Config");
const TimerUtil_1 = require("../src/engine/utils/TimerUtil");
const Logger_1 = require("../src/engine/utils/Logger");
const DbConfig_1 = require("../src/config/DbConfig");
const ServerConfig_1 = require("../src/config/ServerConfig");
const UploadReqHandler_1 = require("./UploadReqHandler");
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const http = require("http");
/**
 */
class upload_server {
    main() {
        return __awaiter(this, void 0, void 0, function* () {
            let port = parseInt(process.argv[2]);
            if (isNaN(port)) {
                Logger_1.Logger.warn("port为空");
                return;
            }
            let environment = parseInt(process.argv[3]);
            if (isNaN(port)) {
                Logger_1.Logger.warn("environment为空");
                return;
            }
            Config_1.Config.environment = environment;
            if (Config_1.Config.environment == Config_1.Config.EnvironmentType.Online) {
                Logger_1.Logger.LEVEL = Logger_1.Logger.LOG_LEVEL_TYPES.DEBUG;
            }
            else {
                Logger_1.Logger.LEVEL = Logger_1.Logger.LOG_LEVEL_TYPES.DEBUG;
            }
            Config_1.Config.serverType = Config_1.Config.ServerType_List.upload_server;
            Logger_1.Logger.tag = Config_1.Config.serverType + "-" + port;
            DbConfig_1.DbConfig.init(environment);
            Config_1.Config.initGameHotupdateVersionConfig();
            yield ServerConfig_1.ServerConfig.initConfig(port);
            TimerUtil_1.TimerUtil.init();
            // Logger.log("config=", configObj[""])
            let app = express();
            let publicPath = path.resolve() + "/public";
            if (!fs.existsSync(publicPath)) {
                fs.mkdirSync(publicPath);
            }
            app.use(bodyParser.urlencoded({ extended: false })); //解析post请求数据
            app.use(bodyParser.text()); // for parsing application/json
            app.use(bodyParser.json()); // for parsing application/json
            //设置静态网页目录
            let options = {
                setHeaders: function (res, path, stat) {
                    res.set('Access-Control-Allow-Origin', '*');
                }
            };
            app.use(express.static(publicPath, options));
            app.use(cookieParser());
            app.all('*', function (req, res, next) {
                req.header("Content-Type", "application/json;charset=UTF-8");
                res.header("Access-Control-Allow-Origin", "*"); //项目上线后改成页面的地址
                res.header("Content-Type", "application/json;charset=UTF-8");
                res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
                next();
            });
            UploadReqHandler_1.UploadReqHandler.instance.init(app);
            http.createServer(app).listen(port);
            Logger_1.Logger.info("start suc=", port);
        });
    }
}
exports.upload_server = upload_server;
upload_server.instance = new upload_server();
try {
    upload_server.instance.main();
}
catch (error) {
    Logger_1.Logger.info("error=", error);
    Logger_1.Logger.error("error=", error);
}
